#include <iostream>
#include <vector>
#include <string>
#include <math.h>

using namespace std;

int findLongest(vector<string> &path){
    int l = 0;
    for(int i = 0; i < path.size(); i++){
        if(path[i].size() > l) l = path[i].size();
    }
    return l;
}

void inputPathToVector(const vector<string> &st, vector<string> &path){
    auto i = st.begin(); string p = *(i++);
    while(i != st.end()){
        p += "/" + *(i++);
    }
    path.push_back(p);
}

int lengthLongestPath(string input) {
    vector<string> st, path; int i = 0, pre = i, t = 0, tpre = -1;
    while(i < input.size()){
        i = input.find("\n", pre);
        // 如果遇到同目录或者需要返回上一目录的情况
        if(t - tpre == 0){
            if((*(st.end() - 1)).find(".") != -1) inputPathToVector(st, path);
            st.pop_back();
        }else if(t - tpre < 0){
            if((*(st.end() - 1)).find(".") != -1) inputPathToVector(st, path);
            for(int j = 0; j <= abs(t - tpre); j++){
                st.pop_back();
            }
        }
        // 插入新的路径
        st.push_back(input.substr(pre, i - pre));
        i++, tpre = t, t = 0;
        if(i == 0){
            i = input.size();
            if((*(st.end() - 1)).find(".") != -1) inputPathToVector(st, path);
        }else{
            while(input.find("\t", i) == i && i < input.size()){
                i++; t++;
            }
        }
        pre = i;
    }
    return findLongest(path);
}

int main(){
    string input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";
    cout << lengthLongestPath(input) << endl;
    return 0;
}
